#!/usr/bin python3
# -*- coding: utf-8 -*-


def finput() -> list:
    n = input()
    a = [int(i) for i in input().split(' ')]
    return a


def analysis(a_0:list)->int:

    a_1 = sorted(a_0)
    mask = []
    for i, j in zip(a_1, a_0):
        if i == j:
            pass
        else:
            mask.append(i)
    mask = sorted(mask)
    temp = sum(mask[0:len(mask)-1])
    if temp > mask[-1]:
        return mask[-1]
    else:
        return temp


if __name__ == "__main__":

    a_0 = finput()
    res = analysis(a_0)
    print(res)